/*
 * $Id: BarGraph.java 41 2013-01-08 04:03:56Z rob@patientsknowbest.com $ 
 */
package com.representqueens.spark;

/*
 * 
 * Copyright 2006 Larry Ogrodnek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * Bargraph generation.
 * 
 * @author Larry Ogrodnek <larry@cheesesteak.net>
 * @author Rob Whelan <rob@patientsknowbest.com>
 * @version $Revision: 41 $ $Date: 2013-01-08 04:03:56 +0000 (Tue, 08 Jan 2013) $
 */
public class BarGraph {
    public static final int DEFAULT_WIDTH = 100;
    public static final int DEFAULT_HEIGHT = 25;

    public static final int DEFAULT_SPACING = 2;

    public static final SizeParams DEFAULT_SIZE = new SizeParams(DEFAULT_WIDTH,
	    DEFAULT_HEIGHT, DEFAULT_SPACING);

    public static final Color DEFAULT_COLOR = Color.GRAY;

    /**
     * Create a Bargraph from an array of data points, using default colors and
     * sizes.
     * 
     * @param data
     *            Array of Number Objects.
     * 
     * @return BufferedImage containing a Bargraph of data.
     */
    public static BufferedImage createGraph(final List<DataPoint> data) {
	return createGraph(data, DEFAULT_SIZE, DEFAULT_COLOR);
    }

    /**
     * Create a Bargraph.
     * 
     * @param data
     *            Array of DataPoint Objects to graph.
     * @param size
     *            SizeParams specifying graph size attributes.
     * @param color
     *            main graph color
     * 
     * @return BufferedImage containing a Bargraph of data.
     */
    public static BufferedImage createGraph(final List<DataPoint> data,
	    final SizeParams size, final Color color) {
	return createGraph(data, size, color, null);
    }

    /**
     * Create a Bargraph. Dropped direct support for highColor and lastColor, as these can
     * now be included in the info for the data point.
     * 
     * @param data
     *            Array of DataPoint Objects to graph.
     * @param size
     *            SizeParams specifying graph size attributes.
     * @param color
     *            main graph color
     * @param background
     *            background color, or null for transparency.
     * 
     * @return BufferedImage containing a Bargraph of data.
     */
    public static BufferedImage createGraph(final List<DataPoint> data,
	    final SizeParams size, final Color color, final Color background) {
	final BufferedImage bi = new BufferedImage(size.getWidth(),
		size.getHeight(), BufferedImage.TYPE_INT_ARGB);

	if (data == null || data.size() < 1) {
	    return bi;
	}

	final Graphics2D g = bi.createGraphics();
	// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	// RenderingHints.VALUE_ANTIALIAS_ON);

	if (background != null) {
	    g.setBackground(background);
	    g.clearRect(0, 0, size.getWidth(), size.getHeight());
	}

	final double yScale = GraphUtils.getGraphInfo(data, size).yScale;

	final int w = (size.getWidth() - (size.getSpacing() * data.size()))
		/ data.size();

	int x = 0;
	int c = 0;

	for (final DataPoint p : data) {
	    final double h = p.getY() * yScale;

	    if( p.getColor() != null )
	    	g.setPaint( p.getColor() );
	    else
	    	g.setPaint( color );

	    g.fill(new Rectangle2D.Double(x, size.getHeight() - h, w, p.getY() * yScale));
	    x += (w + size.getSpacing());
	    c++;
	}

	return bi;
    }

//    private static double getAvg(final List<DataPoint> data) {
//	double total = 0;
//
//	for (final DataPoint p : data) {
//	    total += p.getY();
//	}
//
//	return (total / data.size());
//    }
}
